package com.zrrd.jt_admin.user.controller;


import com.zrrd.jt_admin.common.entity.User;
import com.zrrd.jt_admin.common.response.ResponseResult;
import com.zrrd.jt_admin.common.vo.UserPageVo;
import com.zrrd.jt_admin.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Zhangyk
 * @since 2024-06-30
 */
@Api(tags = "会员管理-接口文档")
@RestController
@RequestMapping("/admin/user")
public class UserController {
    @Resource
    private UserService userService;

    @ApiOperation(value = "会员列表")
    @GetMapping("/")
    @ResponseBody
    public ResponseResult findByPage(@RequestParam("page") Integer page,
                                     @RequestParam(value = "limit",required = false) Integer limit,
                                     @RequestParam(value = "keyword",required = false) String keyword,
                                     @RequestParam(value = "userLevelId",required = false) Integer userLevelId) {
        if(limit==null){
            limit = 10;
        }
        UserPageVo userPageVo = userService.findByPage(page,limit,keyword,userLevelId);
        return ResponseResult.queryResponse(userPageVo);
    }

    @ApiOperation(value = "增加会员")
    @PostMapping("/")
    @ResponseBody
    public ResponseResult addUser(@RequestBody User user) {
        if (user==null){
            return ResponseResult.addResponse(false);
        }
        System.out.println(user);
        boolean flag = userService.save(user);
        return ResponseResult.addResponse(flag);
    }

    @ApiOperation(value = "修改会员")
    @PutMapping("/")
    @ResponseBody
    public ResponseResult updateUser(@RequestBody User user) {
        if (user==null){
            return ResponseResult.updateResponse(false);
        }
        return ResponseResult.updateResponse(userService.updateById(user));
    }

    @ApiOperation(value = "修改会员状态")
    @GetMapping("/{id}/updateStatus")
    @ResponseBody
    public ResponseResult updateUserStatus(@PathVariable("id") Integer id,Integer status) {
        Boolean bStatus=status==1?true:false;
        boolean update = userService.lambdaUpdate().eq(User::getId, id)
                .set(User::getStatus, bStatus)
                .update();
        return ResponseResult.updateResponse(update);
    }

    @ApiOperation(value = "删除会员")
    @DeleteMapping("/{id}/delete")
    @ResponseBody
    public ResponseResult deleteUser(@PathVariable("id") Integer id) {
        return ResponseResult.removeResponse(userService.removeById(id));
    }
}

